const express = require('express');
const router = express.Router();
const loginCheck = require('../middleware/loginCheck');
const {user,diaryManage} = require('../controller/user');
const moment = require('moment');


/* GET users listing. */
router.get('/', loginCheck, function (req, res, next) {
    const result = user(req.session.username);
    return result.then(data => {
        res.render('user/index', {
            title: '用户首页',
            realname: data.realname,
            role: data.name === 'admin' ? '系统管理员' : '用户',
            card: {
                balance: data.balance,
                status: data.status,
            }
        })
    })
});

router.get('/borrow_records', loginCheck, function (req, res, next) {
    const result = user(req.session.username);
    return result.then(data => {
        res.render('user/card_manage/borrow_records', {
            title: '图书馆借阅记录',
            realname: data.realname,
            role: data.name === 'admin' ? '系统管理员' : '用户',
            card: {
                balance: data.balance,
                status: data.status,
            }
        })
    })
});

router.get('/recharge_records', loginCheck, function (req, res, next) {
    const result = user(req.session.username);
    return result.then(data => {
        res.render('user/card_manage/recharge_records', {
            title: '充值记录',
            realname: data.realname,
            role: data.name === 'admin' ? '系统管理员' : '用户',
            card: {
                balance: data.balance,
                status: data.status,
            }
        })
    })
});

router.get('/shop_records', loginCheck, function (req, res, next) {
    const result = user(req.session.username);
    return result.then(data => {
        res.render('user/card_manage/shop_records', {
            title: '消费记录',
            realname: data.realname,
            role: data.name === 'admin' ? '系统管理员' : '用户',
            card: {
                balance: data.balance,
                status: data.status,
            }
        })
    })
});

router.get('/diary_manage', loginCheck, function (req, res, next) {
    const result = diaryManage(req.session.username);
    return result.then(data => {
        res.render('user/school_life/diary_manage', {
            title: '个人日记管理',
            realname: data.realname,
            role: data.name === 'admin' ? '系统管理员' : '用户',
            diary: {
                count: data.length,
                list: data.map(v => {
                    v.createtime = moment(v.createtime).format('YYYY-MM-DD');
                    return v
                })
            }
        })
    })
});

router.get('/diary_add', loginCheck, function (req, res, next) {
    const result = user(req.session.username);
    return result.then(data => {
        res.render('user/school_life/diary_add', {
            title: '个人日记新增',
            realname: data.realname,
            role: data.name === 'admin' ? '系统管理员' : '用户',
        })
    })
});

router.get('/info', loginCheck, function (req, res, next) {
    const result = user(req.session.username);
    return result.then(data => {
        res.render('user/system_config/info', {
            title: '个人信息',
            realname: data.realname,
            role: data.name === 'admin' ? '系统管理员' : '用户',
            card: {
                balance: data.balance,
                status: data.status,
            }
        })
    })
});

router.get('/change_pwd', loginCheck, function (req, res, next) {
    const result = user(req.session.username);
    return result.then(data => {
        res.render('user/system_config/change_pwd', {
            title: '修改密码',
            realname: data.realname,
            role: data.name === 'admin' ? '系统管理员' : '用户',
            card: {
                balance: data.balance,
                status: data.status,
            }
        })
    })
});

module.exports = router;
